// pages/index/subjectToSchool/subjectToSchool.js

import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';

import {
    GetXkjhbkSchoollist, // 查院校
} from '../../../api/index'

Page({

    
    /**
     * 页面的初始数据
     */
    data: {
        listArray: [],
        dataSourceArray: [{
            dictValue: "0",
            dictLabel: "本科"
        },
        {
            dictValue: "1",
            dictLabel: "专科"
        }
    ],
    searchString:"",
    queryType:'bk',
        pageNo:1,
        totalPages:0,
        nomore:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            listArray:[],
            pageNo: 1,
            totalPages: 0,
            nomore: false,
          })
          this.loadRequest(false)
    },

    // tab切换事件
    onChange(event) {
        let tabIndex = event.detail.name;
        console.log(tabIndex)
        if (tabIndex == 0) {
            this.setData({
                queryType: 'bk',
            })
        } else {
            this.setData({
                queryType: 'zk',
            })
        }
        this.setData({
            listArray:[],
            pageNo: 1,
            totalPages: 0,
            nomore: false,
            searchString:'',
          })
          this.loadRequest(false)
    },

    // 输入框输入事件
    inputEvent: function(e) {
        // e.detail.value 包含输入框的当前值
        var value = e.detail.value;
        // 可以根据 field 来区分不同的输入框，这里只有一个输入框
        this.setData({
            searchString: value // 更新对应的data中的值
        });
      },
      // 搜索点击事件
      handleSearch: function(e) {
 
        this.setData({
            listArray:[],
            pageNo: 1,
            totalPages: 0,
            nomore: false,
          })
          this.loadRequest(false)
      },


    

    /** 列表点击事件 */
    cellSelectAction(event) {
        console.log(event)
        let token = wx.getStorageSync('token') || '';
            let openid = wx.getStorageSync('openId') || '';
            if (token == '' || openid == '') {
                wx.navigateTo({
                    url: '/pages/login/login',
                })
                return
            }
        wx.navigateTo({
            url: '/pages/index/schoolSubjectResult/schoolSubjectResult?item=' + JSON.stringify(event.currentTarget.dataset.item) + "&queryType=" + this.data.queryType
        })
    },


    loadRequest(isLoading) {
        
        let url = '?queryType=' + this.data.queryType + "&yxmc=" + this.data.searchString + "&pageNum=" + this.data.pageNo + '&pageSize=10'
        this._GetSchoolList(url, isLoading)
    },


    _GetSchoolList(data, isLoading) {
        console.log(data)
        console.log(this.data.totalPages)
        console.log(this.data.listArray.length)
        let that = this
        if (this.data.totalPages <= this.data.listArray.length && this.data.totalPages != 0) {
            this.setData({
              nomore: true
            })
            return;
          }
          GetXkjhbkSchoollist(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            // Toast('成功')
            if (code == 200) {
                let array = res.rows;
               
                  setTimeout(function() {
                    //要实现延迟执行效果的代码快
                    that.setData({
                        listArray: that.data.listArray.concat(array),
                        pageNo: that.data.pageNo + 1,
                        totalPages: res.total,
                      });
                    }, 100);//设置延迟效果的具体时间
                  if (that.data.totalPages <= that.data.listArray.length && that.data.totalPages != 0) {
                        that.setData({
                            nomore: true
                        })
                  } 
            } else {

                Toast(res.msg)
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
      onPullDownRefresh() {
        console.log('下拉刷新')
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    this.setData({
        listArray:[],
        pageNo: 1,
        totalPages: 0,
        nomore: false,
      })
      this.loadRequest(false)
    // 隐藏导航栏加载框
    wx.hideNavigationBarLoading();
    // 停止下拉动作
    wx.stopPullDownRefresh();
      },


    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        console.log('上拉加载更多')
        this.loadRequest(false)
    },

    onShareTimeline: function (){

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})